package view.list.detailview;



import java.awt.BorderLayout;


import event.Broadcaster;
import event.ChangePanelEvent;
import filesystem.MyFile;
import filesystem.local.MyLocalFileSystem;
import view.list.ListView;
import view.list.PanelChangeRequestListener;

import javax.swing.JComponent;
import javax.swing.JScrollPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;


public class DetailViewPresenter extends ListView {
    MyLocalFileSystem fileSystem = new MyLocalFileSystem();

  private DetailsTableModel model;
  private DetailsTable table=new DetailsTable();
  private int selectedRow=-1;

    /**
	 * Constructor. 
	 */
	public DetailViewPresenter() {
	    try {
	      init();
	    }
	    catch(Exception ex) {
	      ex.printStackTrace();
	    }	    
	}
	
	protected JComponent getViewComponent(){
		if(table==null){
			table=new DetailsTable();
		}
		return table;
	}
	
	private void init() throws Exception{		
		model=new DetailsTableModel(fileSystem);
		table.setModel(model);
		this.setLayout(new BorderLayout());
		scroll = new JScrollPane(table);
//		scroll.getViewport().setBackground(Options.getBackgroundColor());
//		table.setBackground(Options.getBackgroundColor());
//		table.setForeground(Options.getForegroundColor());
//		table.setFont(Options.getPanelsFont());
		
		add(scroll,BorderLayout.CENTER);
		add(topPanel,BorderLayout.NORTH);

//		Broadcaster.addChangeDirectoryListener(new ChangeDirectoryListener(){
//			public void changeDirectory(ChangeDirectoryEvent e){
//				if(e.getSource().equals(model)){
//					statusLabel.setText(e.getDirectory().getAbsolutePath());
//					return;
//				}
//				/*System.out.println("changedir -----");
//				System.out.println(fileSystem);
//				System.out.println(e.getSource());
//				System.out.println((e.getSource() instanceof MkdirAction)+" bool1");
//				System.out.println((isActive)+" bool2");*/
//				if((e.getSource() instanceof javax.swing.Action) && isActive){
//					final DetailsTableModel model=(DetailsTableModel)table.getModel();
//					model.browseDirectory(getCurrentWorkingDirectory());
//					table.getMarkedFiles().clear();
//				}
//
//				//if we were the target of a Copyaction or Move action, then we're not active, but we still should update ourselves
//				if(((e.getSource() instanceof CopyAction)  || (e.getSource() instanceof MoveAction)) && !isActive){
//					final DetailsTableModel model=(DetailsTableModel)table.getModel();
//					model.browseDirectory(getCurrentWorkingDirectory());
//					table.getMarkedFiles().clear();
//				}
//
//			}
//		});
//		table.addFocusListener(new FocusListener(){
//			public void focusGained(FocusEvent e){
//				statusLabel.setForeground(UIManager.getColor("Label.foreground"));
//				if(e.getOppositeComponent()!=null )
//				{
//					Options.setActivePanel(DetailViewPresenter.this);
//				}
//				ChangeDirectoryEvent event=new ChangeDirectoryEvent();
//				event.setDirectory(getCurrentWorkingDirectory());
//				event.setSource(DetailViewPresenter.this);
//				Broadcaster.notifyChangeDirectoryListeners(event);
//				if(selectedRow>=0 && selectedRow<model.getRowCount()){
//					try {
//						table.getSelectionModel().setSelectionInterval(selectedRow,selectedRow);
//					}
//					catch (Exception ignored) { }
//				}
//				table.repaint();
//			}
//			public void focusLost(FocusEvent e){
//				statusLabel.setForeground(UIManager.getColor("Label.disabledForeground"));
//				selectedRow=table.getSelectedRow();
//				table.repaint();
//				// table.getSelectionModel().clearSelection();
//			}
//		});
		table.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
			public void valueChanged(ListSelectionEvent e){
				if(e.getValueIsAdjusting()) return;
				if(table.getSelectionModel().isSelectionEmpty()) return;				
				int[] selections=new int[2];
				int firstIndex = table.getSelectionModel().getMinSelectionIndex();
				int lastIndex = table.getSelectionModel().getMaxSelectionIndex();				
				selections[0]=firstIndex;
				selections[1]=lastIndex;
			}
		});		
//		table.addMouseListener(new MouseAdapter(){
//			public void mouseClicked(MouseEvent event){
//				if(SwingUtilities.isRightMouseButton(event)){
//					if(table.rowAtPoint(event.getPoint())<0){
//						return;
//					}
//					Options.getPopupMenu().show(table,event.getX(),event.getY());
//				}
//			}
//		});
		table.setPanelChangeRequestListener(new PanelChangeRequestListener(){
			public void requestPanelChange() {
				ChangePanelEvent ev=new ChangePanelEvent();
				ev.setSource(DetailViewPresenter.this);
				Broadcaster.notifyChangePanelListeners(ev);
			}
		});
//
		changeDirectory(fileSystem.getStartDirectory());
	}

	protected void changeDirectory(MyFile file){
		model.browseDirectory(file);
	}

  public MyFile getSelectedFile(){
    int row=table.getSelectedRow();
    if(row<0){
      return null;
    }
    return model.getFileAt(row);
  }

  public MyFile[] getSelectedFiles(){

    MyFile[] files=new MyFile[0];
   
    return files;
  }

  public MyFile getCurrentWorkingDirectory(){
    return model.getWorkingDirectory();
  }
  
}
